package com.zshan.clinic.web.controller.medical;


import com.zshan.clinic.admin.entity.dto.OrderPayDTO;
import com.zshan.clinic.admin.entity.vo.ChargeOrderDetailVo;
import com.zshan.clinic.admin.entity.vo.ChargeOrderPageVo;
import com.zshan.clinic.admin.entity.vo.print.ChargeOrderPrintVo;
import com.zshan.clinic.admin.entity.vo.ChargeOrderRecordVo;
import com.zshan.clinic.admin.service.medical.ChargeOrderService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 收费
 */
@RestController
@RequestMapping("/charge/order")
public class ChargeOrderController extends AbstractController {


    @Autowired
    private ChargeOrderService chargeOrderService;


    /**
     * 打印：收费单
     * @param orderId 订单ID
     * @return
     */
    @GetMapping("/getChargeOrderPrint")
    public RestResp<ChargeOrderPrintVo> getChargeOrderPrint(@RequestParam(name = "orderId",required = false) Long orderId,
                                                            @RequestParam(name = "recordId",required = false) Long recordId) {
        UserInfo userInfo = getUserInfo();
        ChargeOrderPrintVo chargeOrderPrintVo = chargeOrderService.getChargeOrderPrint(userInfo.getClinicId(),orderId,recordId,userInfo.getEmpName());
        return RestResp.SUCCESS(chargeOrderPrintVo);
    }

    /**
     * 收费：订单收费记录列表
     * @param orderId
     * @return
     */
    @GetMapping("/getRecordList")
    public RestResp<List<ChargeOrderRecordVo>> getRecordList(@RequestParam(name = "orderId") Long orderId) {
        UserInfo userInfo = getUserInfo();
        List<ChargeOrderRecordVo> recordList = chargeOrderService.getChargeOrderRecordList(userInfo.getClinicId(),orderId);
        return RestResp.SUCCESS(recordList);
    }

    /**
     * 收费：查询诊所今日待收费数
     * @return
     */
    @GetMapping("/getWaitingCount")
    public RestResp<Long> getWaitingCount() {
        UserInfo userInfo = getUserInfo();
        Long waitingCount = chargeOrderService.getWaitingCount(userInfo.getClinicId());
        return RestResp.SUCCESS(waitingCount);
    }

    /**
     * 收费：查询收费单列表
     * @param patientName 患者名
     * @return
     */
    @GetMapping("/getOrderList")
    public RestResp<List<ChargeOrderPageVo>> getOrderList(@RequestParam(name = "visitDate",required = false) LocalDate visitDate,
                                                          @RequestParam(name = "patientName",required = false) String patientName,
                                                          @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<ChargeOrderPageVo> orderList = chargeOrderService.getOrderList(userInfo.getClinicId(),visitDate,patientName,pageNo,pageSize);
        return RestResp.SUCCESS(orderList);
    }


    /**
     * 收费：查询收费单详情
     * @param orderId 订单ID
     * @return
     */
    @GetMapping("/getOrderDetail")
    public RestResp<ChargeOrderDetailVo> getOrderDetail(@RequestParam(name = "orderId") Long orderId) {
        UserInfo userInfo = getUserInfo();
        ChargeOrderDetailVo chargeOrderDetailVo = chargeOrderService.getOrderDetail(userInfo.getClinicId(),orderId);
        return RestResp.SUCCESS(chargeOrderDetailVo);
    }


    /**
     * 收费：收费
     * @param orderPayDTO
     * @return
     */
    @PostMapping("/doCharge")
    public RestResp<Long> doCharge(@RequestBody OrderPayDTO orderPayDTO) {
        UserInfo userInfo = getUserInfo();
        orderPayDTO.setChargeUserId(userInfo.getEmpId());
        orderPayDTO.setChargeBy(userInfo.getEmpName());
        orderPayDTO.setClinicId(userInfo.getClinicId());
        Long recordId = chargeOrderService.doCharge(orderPayDTO);
        return RestResp.SUCCESS(recordId);
    }
}
